package com.ruoyi.service.impl;


import java.lang.ref.WeakReference;
import java.util.*;
import java.util.stream.Collectors;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.schemas.office.office.STInsetMode;
import com.ruoyi.domain.ErpStorageArea;
import org.apache.xmlbeans.impl.xb.xsdschema.LocalSimpleType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ErpWarehouseMapper;
import com.ruoyi.domain.ErpWarehouse;
import com.ruoyi.service.IErpWarehouseService;

/**
 * 仓库基础信息Service业务层处理
 *
 * @author zhp
 * @date 2025-07-18
 */
@Service
public class ErpWarehouseServiceImpl implements IErpWarehouseService {
    @Autowired
    private ErpWarehouseMapper erpWarehouseMapper;


    /**
     * 查询仓库基础信息
     *
     * @param id 仓库基础信息主键
     * @return 仓库基础信息
     */
    @Override
    public ErpWarehouse selectErpWarehouseById(Long id) {
        return erpWarehouseMapper.selectErpWarehouseById(id);
    }

    /**
     * 查询仓库基础信息列表
     *
     * @param erpWarehouse 仓库基础信息
     * @return 仓库基础信息
     */
    @Override
    public List<ErpWarehouse> selectErpWarehouseList(ErpWarehouse erpWarehouse) {
        return erpWarehouseMapper.selectErpWarehouseList(erpWarehouse);
    }

    /**
     * 新增仓库基础信息
     *
     * @param erpWarehouse 仓库基础信息
     * @return 结果
     */
    @Override
    public int insertErpWarehouse(ErpWarehouse erpWarehouse) {
        return erpWarehouseMapper.insertErpWarehouse(erpWarehouse);
    }

    /**
     * 修改仓库基础信息
     *
     * @param erpWarehouse 仓库基础信息
     * @return 结果
     */
    @Override
    public int updateErpWarehouse(ErpWarehouse erpWarehouse) {
        return erpWarehouseMapper.updateErpWarehouse(erpWarehouse);
    }

    /**
     * 批量删除仓库基础信息
     *
     * @param ids 需要删除的仓库基础信息主键
     * @return 结果
     */
    @Override
    public int deleteErpWarehouseByIds(Long[] ids) {
        return erpWarehouseMapper.deleteErpWarehouseByIds(ids);
    }

    /**
     * 删除仓库基础信息信息
     *
     * @param id 仓库基础信息主键
     * @return 结果
     */
    @Override
    public int deleteErpWarehouseById(Long id) {
        return erpWarehouseMapper.deleteErpWarehouseById(id);
    }


    @Override
    //    根据仓库Id查询库区信息
    public List<ErpStorageArea> selectErpStorageAreaById(Long id) {
        return erpWarehouseMapper.selectErpStorageAreaById(id);
    }

    //加载级联选择器的数据
    @Override
    public List<Map<String, Object>> getErpWarehouseAndArea() {
        return erpWarehouseMapper.selectWarehouseWithAreas().stream()
                .map(warehouse -> {
                    Map<String, Object> node = new HashMap<>();
                    node.put("value", warehouse.getId());
                    node.put("label", warehouse.getName());

                    // 安全处理null值，并显式指定类型
                    List<Map<String, Object>> children = Optional.ofNullable(warehouse.getStorageAreas())
                            .orElse(Collections.emptyList())
                            .stream()
                            .map(area -> {
                                Map<String, Object> childMap = new HashMap<>();
                                childMap.put("value", area.getId());
                                childMap.put("label", area.getName());
                                return childMap;
                            })
                            .collect(Collectors.toList());

                    node.put("children", children);
                    return node;
                })
                .collect(Collectors.toList());
    }
}


